<?php

/*
* Class teia\webdev\php\core\ProductItem
* Core class for electronic shopping product-item
*
* @author Bernardo Quibiana
* @date created: 20111030
*
* @date modified: 20111030
* @modified: Bernardo Quibiana
*/

// namespace declaration: Abstract Data Types
namespace teia\webdev\php\core;

require_once 'DisplayableItem.inc';
require_once 'LinkUtil.inc';

class ProductItem extends DisplayableItem {
	protected $prod_obj;
	
	protected $item_id;
	protected $item_name;
	protected $item_size;
	protected $item_color;
	protected $item_qty;
	protected $item_descr;
	
	const IMG_SLIDE_WIDTH = 620;
	const IMG_SLIDE_HEIGHT = 320;
	const IMG_MEDIUM_WIDTH = 600;
	const IMG_MEDIUM_HEIGHT = 310;
	
	/*
	 * Production
	 */
	
	public function __construct($id, $name='', $var_array=NULL, $prod_obj) {
		parent::__construct($id, $name, $var_array);
		$this->prod_obj = $prod_obj;
		$this->db_fetch_imgs();
	}
	
	/**
	 * Generates an array of product-items from the databse
	 * @param Product $prod_obj product object
	 * @param string $item_id item id
	 * @param array $fields relevant ProductItem properties to be fetched
	 * @param array $conds selection conditions
	 * @param int $count expected number of selections
	 * @param string $db_tb name of database table with product-item listings
	 * @throws \Exception throws exception if query or item synthesis fails
	 * @return array associate array of ProductItem objects (key: item id, value: ProductItem object) fetched from the database
	 */
	static function db_factory($prod_obj, $fields=NULL, $conds=NULL, $limit=0, $db_tb='ecom_prod_item') {
		$prod_items = array();
		
		$prod_id = $prod_obj->get_prod_id();
		if (is_null($fields)) $fields = self::default_fields();
		if (is_null($conds)) {
			$conds = array('prod_id' => $prod_id);
		} else {
			$conds = FunctionPool::assoc_prepend($conds, 'prod_id', $prod_id);
		}	
		try {
			// execute query
			$db_link = MySqlLink::link_factory();
			$result = $db_link->select($db_tb, $fields, $conds, $limit);
			if ($result && is_resource($result)) {
				// fetch items
				while ($result_row = mysql_fetch_assoc($result)) {
					$prod_it = self::db_synthesis($result_row, $prod_obj);
					$item_id = $prod_it->get_item_id();
					if ($prod_it) {
						//echo "Item: $item_id<br />";
						$prod_items[$item_id] = $prod_it;
					} else 	{
						throw new \Exception('Product-Item: Unable to synthesize product-item from database data', 210);
					}
				}	
			} else {
				throw new \Exception('Product-Item: Unable to fetch product-items from database', 200);
			}	
		} catch (Exception $e) {
			throw $e;
		}
		//echo "Product: $prod_id; Number of items: " . count($prod_items) . "<br /><br />\n";
		return $prod_items;
	}
	
	static function db_fetch($prod_obj, $item_id, $fields=NULL, $db_tb='ecom_prod_item') {
		$item = null;
		$prod_id = $prod_obj->get_prod_id();
		$limit = 1;
		$conds = array('prod_id' => $prod_id, 'item_id' => $item_id);
		if (is_null($fields)) $fields = self::default_fields();
		if (is_null($conds)) $conds = array();
		$conds['prod_id'] = $prod_id;
		// execute query
		$db_link = MySqlLink::link_factory();
		$result = $db_link->select($db_tb, $fields, $conds, $limit);
		//$result = $db_link->query($query);
		if ($result && is_resource($result)) {
			// read product fields
			$result_row = mysql_fetch_assoc($result);
			// synthesize product object
			$item = self::db_synthesis($result_row, $prod_obj);
		} else {
			throw new \Exception("Product: Unable to fetch product-item '$prod_id-$item_id' from database", 100);
		}
	
		return $item;
	}
	
	/**
	 * Enter description here ...
	 * @param array $tb_row
	 * @return \teia\webdev\php\core\ProductItem
	 */
	static function db_synthesis($tb_row, $prod_obj) {
		$prod_it = new ProductItem(null, null, $tb_row, $prod_obj);
		
		return $prod_it;
	}
	
	static function db_search($conds, $fields=NULL, $db_tb='ecom_prod_item') {
		if (is_null($fields)) $fields = array('prod_id', 'item_id');
		
	}
		
	static function default_fields() {
		return  array('item_id', 'item_name', 'item_size', 'item_color', 'item_qty', 'item_descr');
	}
	
	protected function db_fetch_imgs($fields=NULL, $conds=NULL, $limit=0, $db_tb='ecom_prod_item_img') {
		// prepare query
		$prod_id = $this->prod_obj->get_prod_id();
		$item_id = $this->get_item_id();
		if (is_null($fields)) $fields = array();
		$fields = array_merge($fields, array('img_type', 'img_subtype', 'img_path', 'img_alt_txt'));
		$cols = FunctionPool::array_to_str($fields, ',');
		$default_cond = "prod_id='$prod_id' AND item_id='$item_id'";
		if ($conds) {
			$conds = array_merge($conds, array('prod_id' => $prod_id, 'item_id' => $item_id));
			$where_clause = 'WHERE ' . FunctionPool::assoc_to_str($conds, ' AND ', '\'', '=');
		} else {
			$where_clause = "WHERE prod_id='$prod_id' AND item_id='$item_id'";
		}
		$limit_clause = ($limit > 0) ? "LIMIT $limit" : '';
		// query is ready
		$query = "SELECT $cols FROM $db_tb $where_clause $limit_clause";
		try {
			// execute query
			$db_link = MySqlLink::link_factory();
			$result = $db_link->query($query);
			if ($result && is_resource($result)) {
				// fetch images img_type img_subtype img_path
				while ($result_row = mysql_fetch_assoc($result)) {
					$img_type = $result_row['img_type'];
					$img_subtype = $result_row['img_subtype'];
					$img_path = $result_row['img_path'];
					//unset($result_row['img_type']);
					//unset($result_row['img_subtype']);
					//unset($result_row['img_path']);
					$this->set_img($img_type, $img_subtype, $img_path, $result_row);
				}
			} else {
				throw new \Exception('Product-Item: Unable to fetch product-items from database', 200);
			}
		} catch (Exception $e) {
			// catch only mysql errors
		}
	}
	
	/*
	 * Instance methods
	 */
	public function get_item_id() {
		return $this->item_id;
	}
	
	public function get_item_name() {
		return $this->item_name;
	}
	
	public function get($property) {
		return isset($this->$property) ? $this->$property : null;
	}
	
	public function set_img($img_type, $img_subtype, $img_path, $others=NULL) {
		//echo "--$img_type - $img_subtype - $img_path --<br />";
		switch ($img_type) {
			case 'thumbnail':	$this->thumbnail = $img_path;
			break;
			case 'slide':	$this->slide_img = $img_path;
				break;
			case 'medium':	$this->medium_img = $img_path;
				break;
			case 'small': $this->small_img = $img_path;
				break;
			case 'large': $this->large_img = $img_path;
				break;
			default:
				;
			break;
		}
	}
	
	public function get_link() {
		return LinkUtil::generate_prod_item_link($this->prod_obj, $this);
	}
	
	/*
	 * Display 
	 */
	
	
	public function generate_thumbnail() {
		$link = $this->get_link();
		$thumbnail = $this->get_thumbnail();
		$price = $this->prod_obj->get_prod_price();

		return 
			"<div class=\"place_holder\">
		   		<a href=\"$link\" class=\"price_thumbnail\">
		   			<img src=\"$thumbnail\" class=\"img_thumbnail\" /> 
		   			<p class=\"price_thumbnail\">$$price</p>
		   			<p class=\"name_thumbnail\">$this->item_name</p>
		  			<p class=\"id_thumbnail\">$this->item_id</p>
		   		</a>
			</div>\n";
	}
	
	public function generate_slide() {
		$link = $this->get_link();
		$img = $this->get_slide_image();
		$txt = $this->get_item_name();
		$width = self::IMG_SLIDE_WIDTH;
		$height = self::IMG_SLIDE_HEIGHT;
		 
		return 
			"<div class=\"slide_img\">
		   		<a href=\"$link\" class=\"price_thumbnail\">
		   			<img src=\"$img\" class=\"img_thumbnail\" alt=\"$txt\ width=\"$width\" height=\"$height\" /> 
		   		</a>
			</div>\n";
	}
	
	public function generate_medium_img($show_size=TRUE) {
		$medium_img = $this->get_medium_image();
		$large_img = $this->get_large_image();
		$txt = $this->get_item_name();
		$width = self::IMG_MEDIUM_WIDTH;
		$height = self::IMG_MEDIUM_HEIGHT;
		$img_size = $show_size ? "width=\"$width\" height=\"$height\"" : '';
		
		$content = "<a class=\"thumbnail\" href=\"#\">\n";
		$content .= "<img src=\"$medium_img\" class=\"img_medium\" id=\"_medium_img\" alt=\"$txt\" $img_size />\n";
		$content .= "<span>\n";
		$content .= "<img src=\"$large_img\" class=\"img_large\" id=\"_large_img\" alt=\"$txt\" />\n";
		$content .= "</span>\n";
		$content .= "</a>\n";
		
		return $content;
	}
}

class product_item_t {
	private $prod_id;
	private $item_id;
	
	public function __construct($prod_id, $item_id) {
		$this->prod_id = $prod_id;
		$this->item_id = $item_id;
	}
	
	public function get($property) {
		return isset($this->$property) ? $this->$property : null;
	}
	
	public function get_id() {
		return self::generate_id($this->prod_id, $this->item_id);
	}
	
	static function generate_id($prod_id, $item_id) {
		return $prod_id . "_" . $item_id;
	}
	
	public function fetch_prod_obj() {
		$item_filter = array('item_id' => $this->item_id);
		$prod_obj = Product::db_fetch($this->prod_id, null, $item_filter);
		return $prod_obj;
	}
}

?>